########################################################################################
### Replication Code                                                                 ###
### Title: Overestimation of the Level of Democracy among Citizens in Nondemocracies ###
### Author: Eddy S. F. Yeung                                                         ###
### Version: March 5, 2022                                                           ###
########################################################################################

### Set-up ###
# Clean the R environment and set the working directory
# RStudio version 1.3.959 (macOS)
rm(list = ls())
setwd("~/Desktop/CPS_replication/figures") # save figures in a "figures" folder

# Load the required packages
library(readxl)    # version 1.3.1
library(ggplot2)   # version 3.3.5
library(extrafont) # version 0.17

# Import the dataset
df <- 
  read_excel("~/Desktop/CPS_replication/rob-check_summary.xlsx",
             col_types = c("numeric", "text", "numeric", 
                           "numeric", "numeric", "text"))

# Label the group names
df$rob_check <- 
  factor(df$rob_check,
         levels = c(1:29),
         labels = c("Placebo Test 1\n(DV = Degree of Overestimation)",                        #1
                    "Placebo Test 2\n(DV = Overestimated or Not)",                            #2
                    "Horse Race Test 1\n(Clean Elections Index vs.\nFreedom of the Press)",   #3
                    "Horse Race Test 2\n(Clean Elections Index vs.\nMedia System Freedom)",   #4
                    "Horse Race Test 3\n(Elected Officials Index vs.\nFreedom of the Press)", #5
                    "Horse Race Test 4\n(Elected Officials Index vs.\nMedia System Freedom)", #6
                    "Drop Nonsupporters of Democracy\n(Nonsupporters = 1 to 5)",              #7
                    "Drop Nonsupporters of Democracy\n(Nonsupporters = 1 to 9)",              #8
                    "Liberal Democracy\n(V-Dem)",                                             #9
                    "Participatory Democracy\n(V-Dem)",                                       #10
                    "Deliberative Democracy\n(V-Dem)",                                        #11
                    "Egalitarian Democracy\n(V-Dem)",                                         #12
                    "Polity Score\n(Polity5)",                                                #13
                    "Freedom in the World\n(Freedom House)",                                  #14
                    "Drop Chinese\nRespondents Only",                                         #15
                    "Drop Russian\nRespondents Only",                                         #16
                    "Drop Chinese and\nRussian Respondents",                                  #17
                    "Drop Pakistani, Thai,\nand Ukrainian Respondents",                       #18
                    "Drop High-Income\nRespondents",                                          #19
                    "Drop Privileged\nRespondents",                                           #20
                    "Drop Respondents\nWorried About\nState Monitoring",                      #21
                    "Drop Respondents\nwho Had Other\nPeople Around",                         #22
                    "Drop the Most\nRepressive Regimes",                                      #23
                    "Drop Respondents who View\nDemocracy Differently",                       #24
                    "Ease the Restriction\nfrom 10 to 9",                                     #25
                    "Further Ease the Restriction\nfrom 9 to 8",                              #26
                    "Keep Respondents who\nRate Free Elections\nas Strictly Higher",          #27
                    "Keep Respondents who\nRate Civil Rights\nas Strictly Higher",            #28
                    "Keep Respondents who\nValue Conventional Items\nOver Strange Items"      #29
                    )
         )

# Subset the dataset
df.placebo <- df[1:4, ]
df.horse.race <- df[5:12, ]
df.nonsupport <- df[13:18, ]
df.alt.index <- df[19:36, ]
df.country <- df[37:48, ]
df.pref.fals <- df[49:63, ]
df.diff.view1 <- df[64:72, ]
df.diff.view2 <- df[73:81, ]

### Figure 6 ###
ggplot(data = df.alt.index,
       aes(x = rob_check, y = coefficient, 
           color = covariate, shape = covariate)) +
  geom_point(position = position_dodge(.5), size = 2) +
  scale_color_manual(values = c("grey0", "grey30", "grey60")) +
  scale_shape_manual(values = c(19, 15, 17)) +
  geom_errorbar(aes(ymin = lower_bound, ymax = upper_bound),
                width = 0, position = position_dodge(.5)) +
  xlab("") + 
  ylab("Coefficient Estimate") +
  theme_bw() +
  theme(text = element_text(color = "black", size = 13, family = "Times"),
        axis.text = element_text(color = "black"),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        legend.position = "bottom",
        legend.direction = "horizontal",
        legend.background = element_blank(),
        legend.box.background = element_rect(color = "black"),
        legend.title = element_blank(),
        legend.key.size = unit(1.5, "line"),
        legend.key.height = unit(0, "cm")) +
  geom_hline(yintercept = 0, linetype = "dashed", color = "black") +
  coord_cartesian(ylim = c(-.55, .04)) +
  guides(color = guide_legend(nrow = 1, byrow = T))
ggsave("Figure 6.pdf", width = 9.5, height = 4.25)

### Figure 7 ###
ggplot(data = df.country,
       aes(x = rob_check, y = coefficient, 
           color = covariate, shape = covariate)) +
  geom_point(position = position_dodge(.5), size = 2) +
  scale_color_manual(values = c("grey0", "grey30", "grey60")) +
  scale_shape_manual(values = c(19, 15, 17)) +
  geom_errorbar(aes(ymin = lower_bound, ymax = upper_bound),
                width = 0, position = position_dodge(.5)) +
  xlab("") + 
  ylab("Coefficient Estimate") +
  theme_bw() +
  theme(text = element_text(color = "black", size = 13, family = "Times"),
        axis.text = element_text(color = "black"),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        legend.position = "bottom",
        legend.direction = "horizontal",
        legend.background = element_blank(),
        legend.box.background = element_rect(color = "black"),
        legend.title = element_blank(),
        legend.key.size = unit(1.5, "line"),
        legend.key.height = unit(0, "cm")) +
  geom_hline(yintercept = 0, linetype = "dashed", color = "black") +
  coord_cartesian(ylim = c(-.55, .04)) +
  guides(color = guide_legend(nrow = 1, byrow = T))
ggsave("Figure 7.pdf", width = 8, height = 4)

### Figure S11 ###
ggplot(data = df.diff.view1,
       aes(x = rob_check, y = coefficient, 
           color = covariate, shape = covariate)) +
  geom_point(position = position_dodge(.5), size = 2) +
  scale_color_manual(values = c("grey0", "grey30", "grey60")) +
  scale_shape_manual(values = c(19, 15, 17)) +
  geom_errorbar(aes(ymin = lower_bound, ymax = upper_bound),
                width = 0, position = position_dodge(.5)) +
  xlab("") + 
  ylab("Coefficient Estimate") +
  theme_bw() +
  theme(text = element_text(color = "black", size = 13, family = "Times"),
        axis.text = element_text(color = "black"),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        legend.position = "bottom",
        legend.direction = "horizontal",
        legend.background = element_blank(),
        legend.box.background = element_rect(color = "black"),
        legend.title = element_blank(),
        legend.key.size = unit(1.5, "line"),
        legend.key.height = unit(0, "cm")) +
  geom_hline(yintercept = 0, linetype = "dashed", color = "black") +
  coord_cartesian(ylim = c(-.55, .04)) +
  guides(color = guide_legend(nrow = 1, byrow = T))
ggsave("Figure S11.pdf", width = 8, height = 4)

### Figure S12 ###
ggplot(data = df.diff.view2,
       aes(x = rob_check, y = coefficient, 
           color = covariate, shape = covariate)) +
  geom_point(position = position_dodge(.5), size = 2) +
  scale_color_manual(values = c("grey0", "grey30", "grey60")) +
  scale_shape_manual(values = c(19, 15, 17)) +
  geom_errorbar(aes(ymin = lower_bound, ymax = upper_bound),
                width = 0, position = position_dodge(.5)) +
  xlab("") + 
  ylab("Coefficient Estimate") +
  theme_bw() +
  theme(text = element_text(color = "black", size = 13, family = "Times"),
        axis.text = element_text(color = "black"),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        legend.position = "bottom",
        legend.direction = "horizontal",
        legend.background = element_blank(),
        legend.box.background = element_rect(color = "black"),
        legend.title = element_blank(),
        legend.key.size = unit(1.5, "line"),
        legend.key.height = unit(0, "cm")) +
  geom_hline(yintercept = 0, linetype = "dashed", color = "black") +
  coord_cartesian(ylim = c(-.7, .04)) +
  guides(color = guide_legend(nrow = 1, byrow = T))
ggsave("Figure S12.pdf", width = 8, height = 4)

### Figure S13 ###
ggplot(data = df.nonsupport,
       aes(x = rob_check, y = coefficient, 
           color = covariate, shape = covariate)) +
  geom_point(position = position_dodge(.5), size = 2) +
  scale_color_manual(values = c("grey0", "grey30", "grey60")) +
  scale_shape_manual(values = c(19, 15, 17)) +
  geom_errorbar(aes(ymin = lower_bound, ymax = upper_bound),
                width = 0, position = position_dodge(.5)) +
  xlab("") + 
  ylab("Coefficient Estimate") +
  theme_bw() +
  theme(text = element_text(color = "black", size = 13, family = "Times"),
        axis.text = element_text(color = "black"),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        legend.position = "bottom",
        legend.direction = "horizontal",
        legend.background = element_blank(),
        legend.box.background = element_rect(color = "black"),
        legend.title = element_blank(),
        legend.key.size = unit(1.5, "line"),
        legend.key.height = unit(0, "cm")) +
  geom_hline(yintercept = 0, linetype = "dashed", color = "black") +
  coord_cartesian(ylim = c(-.6, .04)) +
  guides(color = guide_legend(nrow = 1, byrow = T))
ggsave("Figure S13.pdf", width = 6, height = 4)

### Figure S15 ###
ggplot(data = df.placebo,
       aes(x = rob_check, y = coefficient, 
           color = covariate, shape = covariate)) +
  geom_point(position = position_dodge(.5), size = 2) +
  scale_color_manual(values = c("grey0", "grey30")) +
  scale_shape_manual(values = c(19, 15)) +
  geom_errorbar(aes(ymin = lower_bound, ymax = upper_bound),
                width = 0, position = position_dodge(.5)) +
  xlab("") + 
  ylab("Coefficient Estimate") +
  theme_bw() +
  theme(text = element_text(color = "black", size = 13, family = "Times"),
        axis.text = element_text(color = "black"),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        legend.position = "bottom",
        legend.direction = "horizontal",
        legend.background = element_blank(),
        legend.box.background = element_rect(color = "black"),
        legend.title = element_blank(),
        legend.key.size = unit(1.5, "line"),
        legend.key.height = unit(0, "cm")) +
  geom_hline(yintercept = 0, linetype = "dashed", color = "black") +
  coord_cartesian(ylim = c(-.06, .04)) +
  guides(color = guide_legend(nrow = 1, byrow = T))
ggsave("Figure S15.pdf", width = 6, height = 4)

### Figure S16 ###
ggplot(data = df.horse.race,
       aes(x = rob_check, y = coefficient, 
           color = covariate, shape = covariate)) +
  geom_point(position = position_dodge(.5), size = 2) +
  scale_color_manual(values = c("grey0", "grey0", "grey30", "grey30")) +
  scale_shape_manual(values = c(19, 15, 17, 18)) +
  geom_errorbar(aes(ymin = lower_bound, ymax = upper_bound),
                width = 0, position = position_dodge(.5)) +
  xlab("") + 
  ylab("Coefficient Estimate") +
  theme_bw() +
  theme(text = element_text(color = "black", size = 13, family = "Times"),
        axis.text = element_text(color = "black"),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        legend.position = "bottom",
        legend.direction = "horizontal",
        legend.background = element_blank(),
        legend.box.background = element_rect(color = "black"),
        legend.title = element_blank(),
        legend.key.size = unit(1.5, "line"),
        legend.key.height = unit(0, "cm")) +
  geom_hline(yintercept = 0, linetype = "dashed", color = "black") +
  coord_cartesian(ylim = c(-.2, .05)) +
  guides(color = guide_legend(nrow = 1, byrow = T))
ggsave("Figure S16.pdf", width = 8, height = 4.5)

### Figure S17 ###
ggplot(data = df.pref.fals,
       aes(x = rob_check, y = coefficient, 
           color = covariate, shape = covariate)) +
  geom_point(position = position_dodge(.5), size = 2) +
  scale_color_manual(values = c("grey0", "grey30", "grey60")) +
  scale_shape_manual(values = c(19, 15, 17)) +
  geom_errorbar(aes(ymin = lower_bound, ymax = upper_bound),
                width = 0, position = position_dodge(.5)) +
  xlab("") + 
  ylab("Coefficient Estimate") +
  theme_bw() +
  theme(text = element_text(color = "black", size = 13, family = "Times"),
        axis.text = element_text(color = "black"),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        legend.position = "bottom",
        legend.direction = "horizontal",
        legend.background = element_blank(),
        legend.box.background = element_rect(color = "black"),
        legend.title = element_blank(),
        legend.key.size = unit(1.5, "line"),
        legend.key.height = unit(0, "cm")) +
  geom_hline(yintercept = 0, linetype = "dashed", color = "black") +
  coord_cartesian(ylim = c(-.55, .04)) +
  guides(color = guide_legend(nrow = 1, byrow = T))
ggsave("Figure S17.pdf", width = 8, height = 4)
